package com.example.View.Renderers;

import java.util.HashMap;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.R;
import com.example.View.Projection;
import com.example.util.Figure;
import com.example.util.TileType;

public class TileRenderer {

    // The paint to be used for rendering general figures (such as arrows)
    private Paint normalPaint;

    private Map<String, Bitmap> iconByName;

    /**
     * Constructs a GeneralRenderer
     */
    public TileRenderer(Resources resources) {
        normalPaint = new Paint();
        normalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        normalPaint.setARGB(255, 255, 255, 0);
        normalPaint.setAntiAlias(true);
        iconByName = new HashMap<String, Bitmap>();
        iconByName.put("grass", BitmapFactory.decodeResource(resources,R.drawable.grass));
        iconByName.put("road", BitmapFactory.decodeResource(resources,R.drawable.road));
        iconByName.put("sea", BitmapFactory.decodeResource(resources,R.drawable.sea));
        iconByName.put("rocks", BitmapFactory.decodeResource(resources,R.drawable.rocks));
        iconByName.put("mulch", BitmapFactory.decodeResource(resources,R.drawable.mulch));
    }

    /**
     * Renders a given tile
     * @param canvas The canvas on which (and using which) this figure is rendered
     * @param projection The translation between the model coordinate system and the view coordinate system.
     * @param figure The figure to be rendered.
     */
    public void render(Canvas canvas, Projection projection, TileType tileType, int x, int y) {
    	Bitmap bitmap = getBitmap(tileType);
    	canvas.drawBitmap(bitmap, x * projection.getPixelsPerCell() - projection.getX(), y * projection.getPixelsPerCell() - projection.getY(), normalPaint);
	}

    /**
	 * Returns the bitmap to be used for a given building according to player
	 */
    protected Bitmap getBitmap(TileType tileType) {
    	String name = tileType.toString();
        Bitmap bitmap = iconByName.get(name);
        return (bitmap);
    }

}
